package com.ykdl.member.kid.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.TopicAndPostBean;
import com.ykdl.member.kid.beans.TopicBean;

public class PostAdapter extends BaseAdapter {
	
	private Context context;
	private List<TopicAndPostBean> topics = new ArrayList<TopicAndPostBean>();
	private SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
	
	public PostAdapter(){}
	
	public PostAdapter(Context _context){
		this.context = _context;
	}
	
	public void addTopic(List<TopicAndPostBean> list){
		if (list != null && list.size() != 0) {
			for (TopicAndPostBean favoriteBean : list) {
				this.topics.add(favoriteBean);
			}
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return topics.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return topics.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_post, null);
			holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
			holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
			holder.txtDate = (TextView) convertView.findViewById(R.id.txtDate);
			holder.txtCount = (TextView) convertView.findViewById(R.id.txtCount);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		TopicAndPostBean post = (TopicAndPostBean) getItem(position);
		holder.txtTitle.setText(post == null ? "" : post.getMessage());
		holder.txtName.setText(post.getGroup() == null ? "" : post.getGroup().getGroup_name());
		
		long time = (long)(Float.valueOf(TextUtils.isEmpty(post.getPost_time())?"0":post.getPost_time()) * 1000);
		
		holder.txtDate.setText(format.format(new Date(time)));
		holder.txtCount.setText(String.valueOf(post.getLikes_count()));
		return convertView;
	}
	
	class ViewHolder{
		TextView txtTitle;
		TextView txtName;
		TextView txtDate;
		TextView txtCount;
	}

}
